//
// Created by lymk on 2021/8/4.
//

#include "GameActor.h"
#include "ItemActor.h"
#include "iostream"

#pragma   comment(lib,"Msimg32.lib")


void GameActor::HandleInput(InputManager *manager) {
    //std::cout<<animeComponent->getAnimeState()<<std::endl;
    if(manager->GetKeyBoardState(VK_UP) && bCanJump){
        actorSpeed =  GMath::Vec2(actorSpeed.X,-200);
        animeComponent->setAnimeState(JUMP);
        bCanJump = false;
    }else if(manager->GetKeyBoardState(VK_LEFT)){
        actorSpeed =  GMath::Vec2(-200,actorSpeed.Y);
        animeComponent->setAnimeState(WALK);
    }else if(manager->GetKeyBoardState(VK_RIGHT)){
        actorSpeed =  GMath::Vec2(200,actorSpeed.Y);
        animeComponent->setAnimeState(WALK);
    }else{
        if(animeComponent->getAnimeState()!=JUMP)
            animeComponent->setAnimeState(IDLE);
        actorSpeed = {0,actorSpeed.Y};
    }
}

const GMath::Vec2 &GameActor::getRenderPos() const {
    return renderPos;
}

void GameActor::setRenderPos(const GMath::Vec2 &renderPos) {
    GameActor::renderPos = renderPos;
}

const GMath::Vec2 &GameActor::getActorSpeed() const {
    return actorSpeed;
}

void GameActor::setActorSpeed(const GMath::Vec2 &actorSpeed) {
    GameActor::actorSpeed = actorSpeed;
}

const GMath::Vec2 &GameActor::getCollideSize() const {
    return collideSize;
}

void GameActor::setCollideSize(const GMath::Vec2 &collideSize) {
    GameActor::collideSize = collideSize;
}

const GMath::Vec2 &GameActor::getCollidePos() const {
    return collidePos;
}

void GameActor::setCollidePos(const GMath::Vec2 &collidePos) {
    GameActor::collidePos = collidePos;
}

void GameActor::Initialize(std::string path, GMath::Vec2 initPos, GMath::Vec2 sp) {
    animeComponent = new AnimeComponent();
    animeComponent->Initialize(path,0.2,4);

    PicData* tmp = animeComponent->getAnimeData();
    renderPos = initPos;
    collidePos = {initPos.X+tmp->width/16,initPos.Y+tmp->height/16};
    collideSize = {static_cast<float>(tmp->width/8),static_cast<float>(tmp->height/8)};

    actorSpeed = sp;
    score = 0;
    bCanJump = true;
}

void GameActor::update(double DeltaSeconds, RenderUtils renderUtils, InputManager *manager,PhyComponent* worldCollide) {
    //std::cout<<collidePos.X<<' '<<collidePos.Y<<"|"<<collideSize.X<<' '<<collideSize.Y<<std::endl;
    if(actorSpeed.Y == 0)bCanJump = true;
    HandleInput(manager);
    HandleMove(DeltaSeconds,worldCollide);
    animeComponent->update(DeltaSeconds,renderUtils,this);
}

void GameActor::Finalize() {
    animeComponent->Finalize();
}

void GameActor::HandleMove(double DeltaSeconds,PhyComponent *worldCollide) {
    GMath::Vec2 tmpR = renderPos + actorSpeed*DeltaSeconds;
    GMath::Vec2 tmpC = collidePos + actorSpeed*DeltaSeconds;
    ItemActor* tmpItem = worldCollide->PreViewCollide({tmpC.X,tmpC.Y,tmpC.X+collideSize.X,tmpC.Y+collideSize.Y});
    if(tmpItem == nullptr){
        renderPos = tmpR;
        collidePos = tmpC;
        actorSpeed = actorSpeed + GMath::Vec2(0,400) * DeltaSeconds;
    }else if(tmpItem->getPicData() == nullptr){
        actorSpeed = {0,0};
    }else {
        //std::cout<<"AAA"<<std::endl;
        score+=tmpItem->getScore();
        tmpItem->setBShouldDestory(true);
    }

}

float GameActor::getScore() const {
    return score;
}

void GameActor::setScore(float score) {
    GameActor::score = score;
}
